<?php

class Dom_Element {

	public function __construct($name, $value = NULL) {
		$control		=	Control::getControl();
		$this->element	=	$control->createElement($name, $value);
	}
	
	public function __call($a, $b) {
		if(method_exists($this->element, $a) !== FALSE) {
			return call_user_func_array(array($this->element, $a), $b);
		} elseif(method_exists('Dom_Html', $a)) {
			$tag	=	call_user_func_array(array('Dom_Html', $a), $b);
			$this->appendChild($tag->element);

			return $tag;
		} else {
			throw new Exception("Invalid method provided : $a");
		}
	}

	/*
	 * 	setAttributes([array $attributes])						:	Sets an array of attributes to the current element.
	 * 
	 * 	@access	public
	 * 	@param	array		$attributes							-	An array of attributes to apply to the element.
	 */
	public function setAttributes(array $attributes = NULL) {
		if(isset($attributes) && !empty($attributes)) {
    		foreach($attributes as $attribute => $value) {
    			$this->element->setAttribute($attribute, $value);
    		}
		}
	}
	
	/*
	 * 	checkElement(DOMElement $element)						:	Verifies that the supplied argument is a valid object.
	 * 
	 * 	@access	public
	 * 	@param	DOMElement	$element							-	An element to verify the integrity of.
	 * 	@return	bool											-	Returns TRUE if element is a DOMElement, FALSE if not.
	 */
	private function checkElement($element) {
		if(is_object($element) && get_class($element) == "DOMElement") {
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	/*
	 * 	copyAttributes(DOMElement $element)						:	Copies the supplied element's attributes to the current element.
	 * 
	 * 	@access	public
	 * 	@param	DOMElement	$element							-	An element to copy the attributes from.
	 */
	public function copyAttributes($element) {
		if($this->checkElement($element)) {
			if($element->hasAttributes()) {
				foreach($element->attributes as $attribute) {
					$attributes[$attribute->name]	=	$attribute->value;
				}
				
				$this->setAttributes($attributes);
			}
		}
	}
	
	/*
	 * 	copyNodes(DOMElement $element)							:	Copies all of the nodes from the supplied element, into the current element.
	 * 
	 * 	@access	public
	 * 	@param	DOMElement	$element							-	An element to copy the nodes from.
	 */
	public function copyNodes($element) {
		if($this->checkElement($element)) {
			if($element->hasChildNodes()) {
				for($a = 0; $a < $element->childNodes->length; $a++) {
					$curNode	=	$element->childNodes->item($a);
					
					if($this->checkElement($curNode)) {
						$this->appendChild($curNode->cloneNode(TRUE));
					}
				}
			}
		}
	}
	
	/*
	 * 	copyElement(DOMElement $element)						:	Create an element copy with extended functionality.
	 * 
	 * 	@access	public	static
	 * 	@param	DOMElement	$element							-	An element to make the copy of.
	 * 	@return	mixed		$tag								-	Returns a Dom_Element object on success, FALSE on failure.
	 */
	public function copyElement($element) {
		if(self::checkElement($element)) {
			$tag	=	new Dom_Element($element->tagName);
			$tag->copyAttributes($element);
			$tag->copyNodes($element);
			
			return $tag;
		} else {
			return FALSE;
		}
	}
	
	public function __destruct() {
		unset($control);
	}
}
?>